//
//  UITextField+Extension.m
//  CoachAssistant
//
//  Created by XCY–hqF on 16/5/23.
//  Copyright © 2016年 XCY. All rights reserved.
//

#import "UITextField+Extension.h"

@implementation UITextField (Extension)

//当前状态是否是高亮状态
- (BOOL)textFieldPosition
{
    BOOL flag = NO;
    NSString *lang = [self.textInputMode primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [self markedTextRange];
        //获取高亮部分
        UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (position) {
            flag = YES;
        }
    }
    return flag;
}

//限制最大的长度
- (void)textFieldMaxLength:(NSInteger)maxLength maxBlock:(void (^)())maxBlock
{
    NSString *toBeString = self.text;
    BOOL flag = [self textFieldPosition];
    if (!flag) {
        if (toBeString.length > maxLength) {
            self.text = [toBeString substringToIndex:maxLength];
            if (maxBlock) {
                maxBlock();
            }
        }
    }
}

//限制最大的长度
- (void)textFieldMaxLength:(NSInteger)maxLength maxBlock:(void (^)())maxBlock completeBlock:(void (^)())completeBlock
{
    [self textFieldMaxLength:maxLength maxBlock:maxBlock];
    if (completeBlock) {
        completeBlock();
    }
}

//手机3-4-4
- (BOOL)textFieldPhone344FormatInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *text = self.text;
    NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
    string = [string stringByReplacingOccurrencesOfString:@"-" withString:@""];
    if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound || (text.length >= 13 && string.length != 0)) {
        return NO;
    }
    
    text = [text stringByReplacingCharactersInRange:range withString:string];
    text = [text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    
    //开始拼装字符串，3-4-4格式
    NSString *newString = @"";
    NSInteger length = 3;
    while (text.length > 0) {
        NSString *subString = [text substringToIndex:MIN(text.length, length)];
        newString = [newString stringByAppendingString:subString];
        if (subString.length == length) {
            newString = [newString stringByAppendingString:@"-"];
        }
        
        text = [text substringFromIndex:MIN(text.length, length)];
        length = 4;
    }
    newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
    [self setText:newString];
    
    //设置光标位置
    NSInteger index = (range.location == 3 || range.location == 8) ? 2 : 1;
    NSInteger location = 0;
    if (string.length == 0) {
        location = range.location > newString.length ? newString.length : range.location;
    }else{
        location = range.location + index;
    }
    [self setSelectedRange:NSMakeRange(location, 0)];
    return NO;
}

//获取选中的光标
- (NSRange)selectedRange
{
    UITextPosition* beginning = self.beginningOfDocument;
    
    UITextRange* selectedRange = self.selectedTextRange;
    UITextPosition* selectionStart = selectedRange.start;
    UITextPosition* selectionEnd = selectedRange.end;
    
    const NSInteger location = [self offsetFromPosition:beginning toPosition:selectionStart];
    const NSInteger length = [self offsetFromPosition:selectionStart toPosition:selectionEnd];
    
    return NSMakeRange(location, length);
}

//设置选中的光标
- (void)setSelectedRange:(NSRange)range
{
    UITextPosition* beginning = self.beginningOfDocument;
    
    UITextPosition* startPosition = [self positionFromPosition:beginning offset:range.location];
    UITextPosition* endPosition = [self positionFromPosition:beginning offset:range.location + range.length];
    UITextRange* selectionRange = [self textRangeFromPosition:startPosition toPosition:endPosition];
    
    [self setSelectedTextRange:selectionRange];
}

@end
